﻿using System;
using Autodesk.Revit.DB;
using YJKArch.Utils;
using TopologySearch;

namespace ArchPostprocessing.CompoundStructureManage.Utility
{
	public class EdgeInfoCommon
	{
		public static bool IsOverlap(Curve curve1, Curve curve2, double offset)
		{
			if (curve1 == null || curve2 == null)
			{
				return false;
			}
			Pole2D edge = Pole2D.TransformWallToStructure2DPole(curve1, ElementId.InvalidElementId, 0.0, 0.0);
			Pole2D edge2 = Pole2D.TransformWallToStructure2DPole(curve2, ElementId.InvalidElementId, 0.0, 0.0);
			return EdgeInfoCommon.IsOverlap(edge, edge2, offset);
		}

		public static bool IsOverlap(Pole2D edge1, Pole2D edge2, double offset)
		{
			if (edge1 == null || edge2 == null)
			{
				return false;
			}
			if (edge1.IsArcPole != edge2.IsArcPole)
			{
				return false;
			}
			if (edge1.IsArcPole)
			{
				if (!Geometry.IsEqual(edge1.Center, edge2.Center, 0.0001))
				{
					return false;
				}
				if (Geometry.GreaterThan(Math.Abs(edge1.Radius - edge2.Radius), offset, Common.MMToFeet(1.0)))
				{
					return false;
				}
				XYZ pt = edge1.StartPoint;
				XYZ pt2 = edge1.EndPoint;
				if (Geometry.IsEqual(edge1.Normal, -XYZ.BasisZ))
				{
					pt = edge1.EndPoint;
					pt2 = edge1.StartPoint;
				}
				XYZ xyz = edge2.StartPoint;
				XYZ xyz2 = edge2.EndPoint;
				if (Geometry.IsEqual(edge2.Normal, -XYZ.BasisZ))
				{
					xyz = edge2.EndPoint;
					xyz2 = edge2.StartPoint;
				}
				XYZ pt3 = edge1.Center + (xyz - edge1.Center).Normalize() * edge1.Radius;
				XYZ pt4 = edge1.Center + (xyz2 - edge1.Center).Normalize() * edge1.Radius;
				XYZ xyz3 = null;
				XYZ xyz4 = null;
				Geometry.OverlapType overlapType = Geometry.IsOverlapOfTwoArc(EdgeInfoCommon.To2D(pt), EdgeInfoCommon.To2D(pt2), EdgeInfoCommon.To2D(edge1.Center), EdgeInfoCommon.To2D(pt3), EdgeInfoCommon.To2D(pt4), EdgeInfoCommon.To2D(edge2.Center), ref xyz3, ref xyz4, 1E-09);
				return overlapType != Geometry.OverlapType._OT_Linked && overlapType != Geometry.OverlapType._OT_None;
			}
			else
			{
				XYZ startPoint = edge1.StartPoint;
				XYZ endPoint = edge1.EndPoint;
				XYZ startPoint2 = edge2.StartPoint;
				XYZ endPoint2 = edge2.EndPoint;
				if (!Geometry.IsParallel(endPoint - startPoint, endPoint2 - startPoint2, 1E-09))
				{
					return false;
				}
				XYZ xyz5 = Geometry.CalculateFootPoint(startPoint, endPoint, startPoint2);
				if (Geometry.GreaterThan(xyz5.DistanceTo(startPoint2), offset, Common.MMToFeet(1.0)))
				{
					return false;
				}
				XYZ pt5 = Geometry.CalculateFootPoint(startPoint, endPoint, endPoint2);
				XYZ xyz6 = null;
				XYZ xyz7 = null;
				Geometry.OverlapType overlapType2 = Geometry.IsOverlapOfTwoLine(EdgeInfoCommon.To2D(startPoint), EdgeInfoCommon.To2D(endPoint), EdgeInfoCommon.To2D(xyz5), EdgeInfoCommon.To2D(pt5), ref xyz6, ref xyz7, true, 1E-09);
				return overlapType2 != Geometry.OverlapType._OT_Linked && overlapType2 != Geometry.OverlapType._OT_None;
			}
		}

		private static XYZ To2D(XYZ pt)
		{
			return new XYZ(pt.X, pt.Y, 0.0);
		}
	}
}
